﻿#ifndef TEST_INDIRECT_PASS
#define TEST_INDIRECT_PASS


#include "Assets/SceneStreamer/GpuDriven/GpuInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

struct Attributes
{
    float4 positionOS : POSITION;
    float3 normalOS : NORMAL;
    float4 tangentOS : TANGENT;
    float2 texcoord : TEXCOORD0;
    float2 lightmapUV : TEXCOORD1;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
    float4 positionCS : SV_POSITION;
    float2 uv : TEXCOORD0;
    DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
    float3 positionWS : TEXCOORD2;
    float3 normalWS : TEXCOORD3;
    float4 tangentWS : TEXCOORD4; // xyz: tangent, w: sign
    float3 viewDirWS : TEXCOORD5;
    float4 shadowCoord : TEXCOORD6;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};


CBUFFER_START(UnityPerMaterial)
float3 _Color;
CBUFFER_END

Varyings vert(Attributes IN)
{
    Varyings output = (Varyings)0;

    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_TRANSFER_INSTANCE_ID(IN, output);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

    output.uv = IN.texcoord;
    output.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
    output.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
    output.normalWS = TransformObjectToWorldNormal(IN.normalOS.xyz);
    output.tangentWS = float4(TransformObjectToWorldDir(IN.tangentOS.xyz), IN.tangentOS.w);
    OUTPUT_LIGHTMAP_UV(IN.lightmapUV, unity_LightmapST, IN.lightmapUV);
    OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
    return output;
}

float4 frag(Varyings IN) : SV_Target
{
    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);

    float3 normalWS = TransformObjectToWorldNormal(IN.normalWS);
    Light mainLight = GetMainLight();
    float3 lightDirWS = normalize(mainLight.direction);

    float3 color = dot(normalWS, lightDirWS) * mainLight.color.rgb;
    half3 bakedGI = float4(0, 0, 0, 1);// + color + _Color; //= SAMPLE_GI(IN.lightmapUV, IN.vertexSH, IN.normalWS);

    //return float4(color + bakedGI + _Color, 1);

    #ifdef LIGHTMAP_ON
    //bakedGI = SampleLightmap(IN.lightmapUV, IN.normalWS);
    bakedGI += float4(1,0,0,1);
    #endif

    #ifdef INSTANCING_ON
    //bakedGI = SampleSHPixel(IN.vertexSH, IN.normalWS);
    bakedGI += float4(0,1,0,1);
    #elif PROCEDURAL_INSTANCING_ON
    // half3 reflectDir = reflect(-IN.viewDirWS, IN.normalWS);
    // half4 envCol = SAMPLE_TEXTURECUBE(unity_SpecCube0, samplerunity_SpecCube0, reflectDir);
    // bakedGI = DecodeHDREnvironment(envCol, unity_SpecCube0_HDR);
    bakedGI += float4(0,1,1,1);
    #endif

    return float4(bakedGI, 1);
}
#endif
